﻿using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TaoYL.BoosterStation
{
    public class SysAdminService
    {
        /// <summary>
        /// 获取所有用户
        /// </summary>
        /// <returns></returns>
        public List<SysAdmin> QuerySysAdmins()
        {
            string sql = "SELECT * FROM SysAdmin";

            SQLiteDataReader reader = SQLiteHelper.ExecuteReader(sql);
            List<SysAdmin> sysAdmins = new List<SysAdmin>();
            while (reader.Read())
            {
                sysAdmins.Add(new SysAdmin
                {
                    LoginId = Convert.ToInt32(reader["LoginId"]),
                    LoginName = reader["LoginName"].ToString(),
                    LoginPwd = reader["LoginPwd"].ToString(),
                    RoleName = (RoleName)Enum.Parse(typeof(RoleName), reader["RoleName"].ToString())
                });
            }

            reader.Close();
            return sysAdmins;
        }

        /// <summary>
        /// 用户登录验证
        /// </summary>
        /// <param name="sysAdmin"></param>
        /// <returns></returns>
        public SysAdmin AdminLogin(SysAdmin sysAdmin)
        {
            //1.封装sql语句
            string sql = "SELECT * FROM SysAdmin WHERE LoginName=@LoginName AND LoginPwd=@LoginPwd";
            //2.封装参数
            SQLiteParameter[] paramArray = new SQLiteParameter[]
            {
                new SQLiteParameter("@LoginName", sysAdmin.LoginName),
                new SQLiteParameter("@LoginPwd", sysAdmin.LoginPwd)
            };

            //3.执行sql语句
            SQLiteDataReader reader = SQLiteHelper.ExecuteReader(sql, paramArray);

            //4.判断是否有数据
            if (reader.Read())
            {
                //5.如果有数据，返回用户对象
                SysAdmin admin = new SysAdmin
                {
                    LoginId = Convert.ToInt32(reader["LoginId"]),
                    LoginName = reader["LoginName"].ToString(),
                    LoginPwd = reader["LoginPwd"].ToString(),
                    RoleName = (RoleName)Enum.Parse(typeof(RoleName), reader["RoleName"].ToString())
                };
                reader.Close();
                return admin;
            }
            else
            {
                //6.如果没有数据，返回null
                reader.Close();
                return null;
            }
        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="sysAdmin"></param>
        /// <returns></returns>
        public bool AddSysAdmin(SysAdmin sysAdmin)
        {
            string sql = "INSERT INTO SysAdmin (LoginName, LoginPwd, RoleName) VALUES (@LoginName, @LoginPwd, @RoleName)";
            SQLiteParameter[] paramArray = new SQLiteParameter[]
            {
                new SQLiteParameter("@LoginName", sysAdmin.LoginName),
                new SQLiteParameter("@LoginPwd", sysAdmin.LoginPwd),
                new SQLiteParameter("@RoleName", sysAdmin.RoleName.ToString())
            };
            int result = SQLiteHelper.ExecuteNonQuery(sql, paramArray);
            return result > 0;
        }
        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="sysAdmin"></param>
        /// <returns></returns>
        public bool ModifySysAdmin(SysAdmin sysAdmin)
        {
            string sql = "UPDATE SysAdmin SET LoginName=@LoginName, LoginPwd=@LoginPwd, RoleName=@RoleName WHERE LoginId=@LoginId";
            SQLiteParameter[] paramArray = new SQLiteParameter[]
            {
                new SQLiteParameter("@LoginId", sysAdmin.LoginId),
                new SQLiteParameter("@LoginName", sysAdmin.LoginName),
                new SQLiteParameter("@LoginPwd", sysAdmin.LoginPwd),
                new SQLiteParameter("@RoleName", sysAdmin.RoleName.ToString())
            };
            int result = SQLiteHelper.ExecuteNonQuery(sql, paramArray);
            return result > 0;
        }
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="loginId"></param>
        /// <returns></returns>
        public bool DeleteSysAdmin(int loginId)
        {
            string sql = "DELETE FROM SysAdmin WHERE LoginId=@LoginId";
            SQLiteParameter[] paramArray = new SQLiteParameter[]
            {
                new SQLiteParameter("@LoginId", loginId)
            };
            int result = SQLiteHelper.ExecuteNonQuery(sql, paramArray);
            return result > 0;
        }
    }
}
